package com.teacher.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.rewarddistribution.RewardDistributionGetDto;
import com.teacher.pojo.entity.RewardDistribution;
import com.teacher.mapper.RewardDistributionMapper;
import com.teacher.pojo.excel.ClassLevelExcel;
import com.teacher.pojo.excel.RewardDistributionExcel;
import com.teacher.pojo.vo.classlevel.ClassLevelGetVo;
import com.teacher.pojo.vo.rewarddistribution.RewardDistributionGetVo;
import com.teacher.service.IRewardDistributionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teacher.service.IStuClassService;
import com.teacher.service.listener.ExcelClassLevelListener;
import com.teacher.service.listener.ExcelRewardDistributionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 奖学金分配表 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2021-08-23
 */
@Service
public class RewardDistributionServiceImpl extends ServiceImpl<RewardDistributionMapper, RewardDistribution> implements IRewardDistributionService {
    @Autowired
    private TermMapper termMapper;

    @Autowired
    private IStuClassService iStuClassService;

    /**
     * 查询奖学金分配
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<IPage<RewardDistributionGetVo>> getPage(RewardDistributionGetDto dto) {
        Page<RewardDistributionGetVo> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        if (dto.getTermId() == null) {
            dto.setTermId(termMapper.getCurrentTerm());
        }
        IPage<RewardDistributionGetVo> iPage = baseMapper.getPage(page, dto);
        return CommonResult.success(iPage);
    }

    /**
     * 导入奖学金分配
     *
     * @param file
     * @return
     */
    @Override
    public CommonResult<String> upload(MultipartFile file) {
        Long termId = termMapper.getCurrentTerm();
        try {
            EasyExcel.read(file.getInputStream(), RewardDistributionExcel.class,
                    new ExcelRewardDistributionListener(termId, this, iStuClassService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResult.failed("上传失败");
        }
        return CommonResult.success("导入成功");
    }
}
